package com.mall.web.control.app;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mall.client.mall.MallService;
import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.client.user.UserService;
import com.mall.common.util.bit.PropertiesConfigure;
import com.mall.pojo.Mall;
import com.mall.pojo.MallGroupBuy;
import com.mall.pojo.User;

@Controller
@RequestMapping("/data/")
public class DataControl {
	private Logger log = LoggerFactory.getLogger(DataControl.class);

	// private static final String driverClassName = "com.mysql.jdbc.Driver";
	// private static final String url =
	// "jdbc:mysql://192.168.31.187:3306/lihaiyang?characterEncoding=utf-8";
	// private static final String username = "root";
	// private static final String password = "123456";
	private static final String driverClassName = "com.mysql.jdbc.Driver";
	private static String url;
	private static String username;
	private static String password;
	private Connection conn;

	@Autowired
	private PropertiesConfigure propertiesConfigure;
	@Autowired
	private UserService userService;
	@Autowired
	private MallService mallService;
	@Autowired
	private MallGroupbBuyService mallGroupbBuyService;

	/**
	 * 获取数据库连接
	 * 
	 * @return
	 */
	public Connection getConnection() {

		if (StringUtils.isBlank(url)) {
			url = propertiesConfigure.getProperties("r.db.url").replaceAll("mall", "lihaiyang");
		}
		if (StringUtils.isBlank(username)) {
			username = propertiesConfigure.getProperties("r.db.username");
		}
		if (StringUtils.isBlank(password)) {
			password = propertiesConfigure.getProperties("r.db.password");
		}

		if (this.conn == null) {
			try {
				Class.forName(driverClassName);
				conn = DriverManager.getConnection(url, username, password);
			} catch (Exception e) {
			}
		}
		return conn;
	}

	@RequestMapping("userdata")
	public void userdata(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Connection conn = getConnection();
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		if (conn == null) {
			return;
		}
		String sql = "select * from user";
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			User user = null;
			while (rs.next()) {
				user = new User();
				user.setCommOnlyKey(1);
				// user.setAreaId(areaId);
				user.setProvinceId(210000);
				user.setCityId(211100);
				user.setGmtCreate(rs.getDate("gmt_create"));
				user.setGmtModify(rs.getDate("gmt_modify"));
				user.setId(rs.getLong("id"));
				user.setIntegral(0l);
				user.setLevel((short) 0);
				user.setPassword(rs.getString("password"));
				user.setPhone(rs.getString("phone"));
				user.setUserName(rs.getString("user_name"));
				user.setNameAndPhone(user.getPhone() == null ? ""
						: user.getPhone() + user.getUserName() == null ? "" : user.getUserName());
				user.setPortrait(rs.getString("portrait")==null?"user/nan.png":rs.getString("portrait"));
				user.setSex((short) rs.getInt("sex"));
				user.setStatus((short) 0);
				user.setType(0);
				user.setAttribute(rs.getString("attribute"));
				int succ = this.userService.insertUserByExample(user);
				if (succ <= 0) {
					log.error("user error id = " + user.getId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
	}
	
	
	
	@RequestMapping("malldata")
	public void malldata(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Connection conn = getConnection();
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		if (conn == null) {
			return;
		}
		String sql = "select * from mall";
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			Mall mall = null;
			while (rs.next()) {
				mall = new Mall();
				mall.setCommOnlyKey(1);
				// user.setAreaId(areaId);
				mall.setProvinceId(210000);
				mall.setCityId(211100);
				mall.setGmtCreate(rs.getDate("gmt_create"));
				mall.setGmtModify(rs.getDate("gmt_modify"));
				mall.setId(rs.getLong("id"));
				mall.setAreaId(rs.getInt("city_area"));
				mall.setAttribute(rs.getString("attribute"));
				mall.setBiaoqian(rs.getString("biaoqian"));
				mall.setCommentCount(rs.getInt("comment_count"));
				mall.setContactName(rs.getString("contact_name"));
				mall.setContactPhone(rs.getString("contact_phone"));
				mall.setCreateUserId(rs.getLong("user_id"));
				mall.setDitie(rs.getString("ditie"));
				mall.setGaodeLatitude(rs.getDouble("gaode_latitude"));
				mall.setGaodeLongitude(rs.getDouble("gaode_longitude"));
				mall.setGongjiao(rs.getString("gongjiao"));
				mall.setLabel(rs.getString("label"));
				mall.setMainPic(rs.getString("main_pic"));
				mall.setMallAddress(rs.getString("mall_address"));
				mall.setMallCategoryId(rs.getInt("mall_category_id"));
				mall.setMallCategoryName(rs.getString("mall_category_name"));
				mall.setMallDesc(rs.getString("mall_desc"));
//				mall.setMallEndTime(rs.getString("mall_end_time"));
				mall.setMallFullName(rs.getString("mall_name"));
				mall.setMallName(rs.getString("mall_name"));
				mall.setMallPerCapita(rs.getInt("mall_per_capita"));
				mall.setMallSecCategoryId(rs.getInt("mall_sec_category_id"));
				mall.setMallSecCategoryName(rs.getString("mall_sec_category_name"));
				mall.setMallStartDay(rs.getString("mall_start_day"));
//				mall.setMallStartTime(rs.getString("mall_start_time"));
				mall.setMallSubName(rs.getString("mall_sub_name"));
				mall.setMallTag(rs.getLong("mall_tag"));
				mall.setMallType(0);
				mall.setOtherPic(rs.getString("other_pic"));
				mall.setPingtaiPingFen(rs.getInt("gov_ping_fen"));
				mall.setPyName(rs.getString("py_name"));
				mall.setQrcodePic(rs.getString("qrcode_pic"));
				mall.setSellerId(rs.getLong("user_id"));
				mall.setSsig(rs.getLong("ssig"));
				mall.setStatus(rs.getInt("status"));
				mall.setThirdId(rs.getLong("third_id"));
				mall.setThirdType(rs.getInt("third_type"));
				mall.setTingchewei(rs.getString("tingchewei"));
				mall.setType(rs.getInt("type"));
				mall.setUserPingFen(rs.getInt("user_ping_fen"));
				int succ = this.mallService.insertMall(mall);
				if (succ <= 0) {
					log.error("user error id = " + mall.getId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
	}
	
	@RequestMapping("mallGroupBuy")
	public void mallGroupBuy(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Connection conn = getConnection();
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		if (conn == null) {
			return;
		}
		String sql = "select * from mall_group_buy";
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			MallGroupBuy mallGroupBuy = null;
			while (rs.next()) {
				mallGroupBuy = new MallGroupBuy();
				mallGroupBuy.setCommOnlyKey(1);
				// user.setAreaId(areaId);
				mallGroupBuy.setProvinceId(210000);
				mallGroupBuy.setCityId(211100);
				mallGroupBuy.setGmtCreate(rs.getDate("gmt_create"));
				mallGroupBuy.setGmtModify(rs.getDate("gmt_modify"));
				mallGroupBuy.setId(rs.getLong("id"));
//				mallGroupBuy.setAreaId(rs.getInt(""));
				mallGroupBuy.setAttribute(rs.getString("attribute"));
				mallGroupBuy.setBiaoqian(rs.getString("biaoqian"));
				mallGroupBuy.setCategoryId(rs.getInt("category_id"));
				mallGroupBuy.setCreateUserId(rs.getLong("user_id"));
				mallGroupBuy.setDiscountPrice(rs.getLong("discount_price"));
				mallGroupBuy.setEndTime(rs.getDate("end_time"));
				mallGroupBuy.setExceptionDate(rs.getString("exception_date"));
				mallGroupBuy.setExpiryDate(rs.getString("expiry_date"));
				mallGroupBuy.setGuizeTixing(rs.getString("guize_tixing"));
				mallGroupBuy.setMainPic(rs.getString("main_pic"));
				mallGroupBuy.setMallId(rs.getLong("mall_id"));
				mallGroupBuy.setOriginalPrice(rs.getLong("original_price"));
//				mallGroupBuy.setOtherMessJson();
				mallGroupBuy.setPrice(rs.getLong("price"));
				mallGroupBuy.setPyName(rs.getString("py_name"));
				mallGroupBuy.setSaleNo(rs.getLong("sale_no"));
				mallGroupBuy.setSecCategoryId(rs.getInt("sec_category_id"));
				mallGroupBuy.setShangjiafuwu(rs.getString("shangjiafuwu"));
				mallGroupBuy.setSimpleDescription(rs.getString("simple_description"));
				mallGroupBuy.setStartTime(rs.getDate("start_time"));
				mallGroupBuy.setStatus(rs.getInt("status"));
				mallGroupBuy.setTitle(rs.getString("title"));
				mallGroupBuy.setTuanDesc(rs.getString("tuan_desc"));
				mallGroupBuy.setTuigaishuoming(rs.getString("tuigaishuoming"));
				mallGroupBuy.setType(rs.getInt("type"));
				mallGroupBuy.setUseTime(rs.getString("use_time"));
				mallGroupBuy.setWenxingtixing(rs.getString("wenxingtixing"));
				mallGroupBuy.setYuyueTixing(rs.getString("yuyue_tixing"));
				int succ = this.mallGroupbBuyService.insertMallGroupBuy(mallGroupBuy);
				if (succ <= 0) {
					log.error("user error id = " + mallGroupBuy.getId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
	}
}
